/**
 * 显示会员列表名称
 */

function sendUser(){
	$.ajax({
		type: "POST",
		url: "/sys_admin/user/showUsername",
		success: function(ret){
				$('#dialog').dialog({
					width:650,
					height:600,
					title:'会员列表',
					buttons:{"确定":function(){form_username_submit();},"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#view_response").html(ret);
			
		},
		dataType: "text"
	});	
}

function form_username_submit(){
	aid = getChecked();
	aid = aid.toString();
	
	uid = getCheckedName();
	uid = uid.toString();
	
	if(aid == '') {
		alert('请选择要发送的用户');
		return false;
	}
	
	
	$("#collectname").val(aid);
	$("#collectnamestr").val(uid);
	
	$("#dialog").dialog("close");
}


/**
 * 显示用户详情的信息
 * @param user_id 用户ID
 * @returns
 */

function showUser(user_id){
	$.ajax({
		type: "POST",
		url: "/sys_admin/user/showDetailInfo",
		data: "user_id="+user_id,
		success: function(ret){			
				$('#dialog').dialog({
					width:650,
					height:600,
					title:'用户信息',
					buttons:{"确定":function(){$(this).dialog("close");}},
				}); 
				$("#dialog").html(ret);
			
		},
		dataType: "text"
	});
	
}


/**
 * 背景调查审核详细信息
 * @param id 调查审核ID
 * @returns
 */
function showFsubject(id){
	$.ajax({
		type: "POST",
		url: "/sys_admin/conset/view_subject",
		data: "id="+id,
		success: function(ret){
				$('#userdialog').dialog({
					width:650,
					height:600,
					title:'背景调查审核详细信息',
					buttons:{"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#view_response").html(ret);
			
		},
		dataType: "text"
	});
	
}


/**
 * 发送站内信、邮件、短信选中所有
 * @returns
 */

function checkAll(o){
	if( o.checked == true ){
		$('input[name="checkbox"]').attr('checked','true');
		$('tr[overstyle="on"]').addClass("bg_on");
	}else{
		$('input[name="checkbox"]').removeAttr('checked');
		$('tr[overstyle="on"]').removeClass("bg_on");
	}
}

function checkon(o){
	if( o.checked == true ){
		$(o).parents('tr').addClass('bg_on') ;
	}else{
		$(o).parents('tr').removeClass('bg_on') ;
	}
}

function getChecked() {
	var gids = new Array();
	$.each($('input:checked'), function(i, n){
		if($(n).val()!=0) gids.push( $(n).val() );
	});

	return gids;
}

function getCheckedName() {
	var uids = new Array();
	$.each($('input:checked'), function(i, n){
		if($(n).val()!=0) uids.push( $(n).attr('dataname'));
	});

	return uids;
}


/**
 * 批量发送站内信、邮件、短信
 * @returns
 */
function sendAll() {
	aid = getChecked();
	aid = aid.toString();
	if(aid == '') {
		alert('请选择要发送的用户');
		return false;
	}

	$.ajax({
		type: "POST",
		url: "/sys_admin/user/send",
		data: "uidstr="+aid,
		success: function(ret){
				$('#senddialog').dialog({
					width:800,
					height:600,
					title:'发送信息',
					buttons:{"提交":function(){form_all_submit();},"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#view_send_response").html(ret);
		},
		dataType: "text"
	});
}

function form_all_submit(){
	var options = {
			url : '/sys_admin/user/doSend/',
			dataType: "json",
			type:"post",
			success : function(ret) {
				if(ret.msg == 'succ'){
					alert("发送成功");
					window.location.href='/sys_admin/user/ulist/';
				}else{
					alert(ret.msg);
				}
				
			}
		};
		$("#sendForm").ajaxSubmit(options);
		return false;
}


/**
 * 单个发送站内信、邮件、短信
 * @returns
 */
function doSend(uid){

	$.ajax({
		type: "POST",
		url: "/sys_admin/user/send",
		data: "uid="+uid,
		success: function(ret){
				$('#senddialog').dialog({
					width:800,
					height:600,
					title:'发送信息',
					buttons:{"提交":function(){form_submit();},"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#view_send_response").html(ret);
		},
		dataType: "text"
	});
}

function form_submit(){
	var options = {
			url : '/sys_admin/user/doSingleSend/',
			dataType: "json",
			type:"post",
			success : function(ret) {
				if(ret.msg == 'succ'){
					alert("发送成功");
					window.location.href='/sys_admin/user/ulist/';
				}else{
					alert(ret.msg);
				}
				
			}
		};
		$("#sendForm").ajaxSubmit(options);
		return false;
}


function templetContent(type){
	var templetId = 0;
	if(type == 3){
		templetId = $("#msgtemplet").val();
	}else if(type == 1){
		templetId = $("#emailtemplet").val();
	}else if(type == 2){
		templetId = $("#smstemplet").val();
	}
		
	$.ajax({
		type: "POST",
		url: "/sys_admin/user/change_send_content",
		data: "id="+templetId,
		success: function(ret){
			if(type == 3){
				$("#msgcontent").val(ret);
			}else if(type == 1){
				$("#emailcontent").val(ret);
			}else if(type == 2){
				$("#smscontent").val(ret);
			}
			
		},
		dataType: "text"
	});
	
}


function add_fsubject(sid){
	$.ajax({
		type: "POST",
		url: "/sys_admin/borrow/upload_view",
		data: "sid="+sid,
		success: function(ret){
				$('#fsubjectdialog').dialog({
					width:800,
					height:600,
					title:'上传文件',
					buttons:{"提交":function(){fsubject_submit(sid);},"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#borrow_upload_view").html(ret);
		},
		dataType: "text"
	});	 
}

function fsubject_submit(sid){
	var options = {
		url : '/sys_admin/borrow/doupload',
		clearForm :true,
		resetForm :true,
		dataType: "json",
		type:"post",
		contentType:"multipart/form-data",
		success : function(ret) {	
			if(ret.url=='error'){
				alert(ret.msg);
			}else{
				$("#fsubjectdialog").dialog("close");
				$("#file_att_"+sid).append(ret.msg.info);
			}
					
			
		}
	};
	$("#upload_form").ajaxSubmit(options);
	return false;
}


function bgSubject(_this){
	var sid = $(_this).val();
	
	if( _this.checked == true ){
		$("#background_investigation_"+sid).show();
	}else{
		$("#background_investigation_"+sid).hide();
	}	
}

// by liub 20150202
$(document).ready(function() {
    //绑定下拉框change事件，当下来框改变时调用 SelectChange()方法
    $("#ftype").change(function() { SelectChange(); }); 
});

// TODO 20150410
function SelectChange() {
    //获取下拉框选中项的value属性值
    var selectValue = $("#ftype").val();
    if( 1 == selectValue){// 
    	$("#bill_piao").css('display','inline'); 
    	$("#bill_hong").css('display','none');
    }else if(0 == selectValue){// 默认
    	$("#bill_piao").css('display','none');
    	$("#bill_hong").css('display','none');
    }else {// 其他类型
    	$("#bill_piao").css('display','none');
    	$("#bill_hong").css('display','inline');
    }
}


function billLimitTimes(){
	var billtime = $("#time_limit").val();
	var bill_starttime = $("#bill_starttime").val();
	$.ajax({
		type: "post",
		url: "/sys_admin/borrow/billtimes/",
		data: "billtime="+billtime+"&bill_starttime="+bill_starttime,
		success: function(ret){
			$("#bill_times").val(ret.info);
			$("#each_time").val(ret.retinfo);
		},
		dataType: "json"
	});
		
	
}



function billTimes(){
	var billtime = $("#time_limit_day").val();
	var bill_starttime = $("#bill_starttime").val();
	$.ajax({
		type: "post",
		url: "/sys_admin/borrow/billtimes/",
		data: "billtimeday="+billtime+"&bill_starttime="+bill_starttime,
		success: function(ret){
			$("#bill_times").val(ret.info);
			$("#each_time").val(ret.retinfo);
		},
		dataType: "json"
	});
		
	
}


function checkBorrowForm(){
	var err = '';
	if($('#ftype').val()=='请选择标产品'){
		err = '选择标产品';
	}else if(IsDouble($('#account').val())==false){
		err = '请填写有效的融资金额';
	}

	else if(IsDouble($('#apr').val())==false){
		err = '请填写有效的收益利率';
	}else if($('#style').val()==''){
		err = '请选择还款方式';
	}
	if(err==''){
		return true;
	}else{
		alert(err);
		return false;
	}
}


function baward(_this){
	var award = $(_this).val();
	if(award == 1){
		$("#fundsname").html('%');
		$("#fundaward").html('比例');
		
	}else{
		$("#fundsname").html('元');
		$("#fundaward").html('金额');
	}
	
}


function subject_remove(id){
	$("#att_litpic_"+id).remove();
}


function addForum(id){
	$.ajax({
		type: "POST",
		url: "/sys_admin/forum/forum_upload_view",
		data: "id="+id,
		success: function(ret){
				$('#dialog').dialog({
					width:800,
					height:600,
					title:'添加主题',
					buttons:{"提交":function(){addsubmitForum(id);},"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#theme_upload_view").html(ret);
		},
		dataType: "text"
	});	 
}

function addsubmitForum(){
	if($("#name").val() == ""){
		$("#upload_error").html('请输入主题名称');
		return false;
	}
	
	if($("#description").val() == ""){
		$("#upload_error").html('请输入主题描述');
		return false;
	}
	
	var options = {
			url : '/sys_admin/forum/forum_save',
			clearForm :true,
			resetForm :true,
			dataType: "json",
			type:"post",
			contentType:"multipart/form-data",
			success : function(ret) {	
				$("#msg").html(ret.msg);		
				setTimeout(function(){
					$("#dialog" ).dialog("close");
				},3000);
				window.location.reload();
			}
		};
		$("#upload_form").ajaxSubmit(options);
		return false;	
}

function removeForum(id){
	if(confirm('你确定要删除?'))
	{
		if(id > 0){
			$.ajax({
				type: "POST",
				url: "/sys_admin/forum/forum_save",
				data: "act=delete&id="+id,
				success: function(ret){
					window.location.reload();
				},
				dataType: "json"
			});	
		}	
	}	
}

/**
 * 是否推荐
 * @returns
 */
function forumFirst(id){
	$.ajax({
		type: "POST",
		url: "/sys_admin/forum/forum_turn",
		data: "act=first&id="+id,
		success: function(ret){
			window.location.reload();
		},
		dataType: "json"
	});	
}

/**
 * 是否屏蔽
 * @returns
 */
function forumShield(id){
	$.ajax({
		type: "POST",
		url: "/sys_admin/forum/forum_turn",
		data: "act=shield&id="+id,
		success: function(ret){
			window.location.reload();
		},
		dataType: "json"
	});	
}






//****** by liub 20141125 查看推广明细汇总
function queryDetail(uid){
	//alert(uid);
	$.ajax({
		type: "POST",
		url: "/sys_admin/cps/query",
		data: "uid="+uid,
		success: function(ret){
				$('#querydialog').dialog({
					width:800,
					height:600,
					title:'查看明细',
					buttons:{"关闭":function(){$(this).dialog("close");}},
				}); 
				$("#view_query_response").html(ret);
		},
		dataType: "text"
	});
}



/**
 * 撤标
 * @param borrow_id
 * @returns
 */
function withdraw(borrow_id){
	$.ajax({
		type: "get",
		url: "/sys_admin/borrow/withdraw/"+borrow_id,
		success: function(msg){
			$("#dialog" ).dialog();
			if(msg == 1){
				$("#msg").html("撤标成功！");
				window.location.reload();
			}else{
				$("#msg").html("撤标失败，请联系管理员！");
			}
			setTimeout(function(){
				$("#dialog" ).dialog("close");
			},3000);
		},
		dataType: "text"
	});
}

function href(url){
	window.location.href=url;
}